using System;

namespace PDUConverter
{
    /// <summary>
    /// Represents the the first octet of an SMS-DELIVER PDU.
    /// </summary>
    public class SmsDeliverMessageFlags : IncomingMessageFlags
    {
        private bool moreMessages;
        private bool replyPathExists;
        private bool statusReportRequested;
        private const byte TP_MMS = 4;
        private const byte TP_MTI_SMS_Deliver = 0;
        private const byte TP_RP = 0x80;
        private const byte TP_SRI = 0x20;
        private const byte TP_UDHI = 0x40;
        private bool userDataHeaderPresent;

        /// <summary>
        /// Initializing a new instance of the class.
        /// </summary>
        public SmsDeliverMessageFlags()
        {
            this.moreMessages = false;
            this.statusReportRequested = false;
            this.userDataHeaderPresent = false;
            this.replyPathExists = false;
        }

        /// <summary>
        /// Initializes a new instance of the SmsDeliverMessageFlags class with a
        /// predefined data byte.
        /// </summary>
        public SmsDeliverMessageFlags(byte flags)
        {
            this.FromByte(flags);
        }

        protected override void FromByte(byte b)
        {
            if (IncomingMessageType.SmsDeliver != IncomingMessageType.SmsDeliver)
            {
                throw new ArgumentException("Not an SMS-DELIVER message.");
            }
            this.moreMessages = (b & 4) > 0;
            this.StatusReportRequested = (b & 0x20) > 0;
            this.userDataHeaderPresent = (b & 0x40) > 0;
            this.replyPathExists = (b & 0x80) > 0;
        }

        /// <summary>
        /// Returns the byte equivalent of this instance.
        /// </summary>
        /// <returns>The byte value.</returns>
        public override byte ToByte()
        {
            byte num = 0;
            if (moreMessages)
                num = (byte) (num | 4);            
            if (StatusReportRequested)
                num = (byte) (num | 0x20);
            if (userDataHeaderPresent)
                num = (byte) (num | 0x40);
            if (replyPathExists)
                num = (byte) (num | 0x80);
            return num;
        }

        /// <summary>
        /// Gets the type of the message.
        /// </summary>
        /// <remarks>Always returns <see cref="F:GsmComm.PduConverter.IncomingMessageType.SmsDeliver" />.</remarks>
        public override IncomingMessageType MessageType
        {
            get
            {
                return IncomingMessageType.SmsDeliver;
            }
        }

        /// <summary>
        /// Gets or sets if there are more messages to send.
        /// </summary>
        public bool MoreMessages
        {
            get
            {
                return this.moreMessages;
            }
            set
            {
                this.moreMessages = value;
            }
        }

        /// <summary>
        /// Gets or sets if a reply path exists.
        /// </summary>
        public bool ReplyPathExists
        {
            get
            {
                return this.replyPathExists;
            }
            set
            {
                this.replyPathExists = value;
            }
        }

        /// <summary>
        /// Gets or sets if a status report was be requested.
        /// </summary>
        public bool StatusReportRequested
        {
            get
            {
                return this.statusReportRequested;
            }
            set
            {
                this.statusReportRequested = value;
            }
        }

        /// <summary>
        /// Gets or sets if a user data header is present.
        /// </summary>
        public bool UserDataHeaderPresent
        {
            get
            {
                return this.userDataHeaderPresent;
            }
            set
            {
                this.userDataHeaderPresent = value;
            }
        }
    }
}